import Order from "../../model/order";
import {getEventParam} from "../../utils/utils";

const order = new Order()

Page({
  data: {
    tabs: ['全部订单', '待同意', '待支付', '待确认', '待评价'],
    currentTabIndex: 0,
    role: null,
    status: null
  },
  onLoad: function (options) {
    const role = parseInt(options.role)
    const status = parseInt(options.status)

    // status: -1:全部  0：待同意、1:待支付、2：待确认、3：待评价
    // tabs:    0：全部  1：待同意、 2、待支付、 3 待确认 4 待评价
    this.setData({
      currentTabIndex: status + 1,
      role
    })
    this.data.status = status < 0 ? '' : status
    this._getOrderlist()
  },

  async _getOrderlist() {
    const orderList = await order.reset().getMyOrderList(this.data.role, this.data.status)
    this.setData({
      orderList
    })
  },

  async handleTabChange(event) {
    const index = getEventParam(event, 'index')
    this.data.status = index < 1 ? '' : index - 1
    await this._getOrderlist()
  },

  async onPullDownRefresh() {
    await this._getOrderlist()
    wx.stopPullDownRefresh()
  },

  async onReachBottom() {
    if (!order.hasMoreData) {
      return
    }
    const orderList = await order.getMyOrderList(this.data.role, this.data.status)
    this.setData({
      orderList
    })
  }
});